package eu.danielhernandez.ejemplos.nombreceltibero.generatedsource;

// Generated 01-mar-2011 14:00:45 by Hibernate Tools 3.3.0.GA

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Home object for domain model class Sonidos.
 * @see eu.danielhernandez.ejemplos.nombreceltibero.generatedsource.Sonidos
 * @author Hibernate Tools
 */
public class SonidosHome {

	private static final Log log = LogFactory.getLog(SonidosHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void persist(Sonidos transientInstance) {
		log.debug("persisting Sonidos instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Sonidos persistentInstance) {
		log.debug("removing Sonidos instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Sonidos merge(Sonidos detachedInstance) {
		log.debug("merging Sonidos instance");
		try {
			Sonidos result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Sonidos findById(Integer id) {
		log.debug("getting Sonidos instance with id: " + id);
		try {
			Sonidos instance = entityManager.find(Sonidos.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public Sonidos findBySoSonido(String sonido){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");  
        entityManager = emf.createEntityManager();  
		log.debug("getting Sonidos instance with soSonido: " + sonido);
		List<Sonidos> sonidos = new ArrayList<Sonidos>();
		
		try{
			sonidos = entityManager.createQuery("from Sonidos where soSonido='" + sonido + "'").getResultList();
			if(sonidos.size()>0)
				return sonidos.get(0);
			else
				return new Sonidos();
		}catch (RuntimeException re){
			log.error("get failed", re);
			throw re;
		}finally{
			entityManager.close();
			emf.close();
		}
	}
}
